export default {
	toast: function(title, duration = 5000) {
		uni.showToast({
			title: title,
			icon: "none",
			duration
		})
	},
	success: function(title) {
		uni.showToast({
			title: title,
			icon: "success"
		})
	},
	loading: function(title) {
		uni.showToast({
			title: title,
			icon: 'loading',
			duration: 10000
		})
	},
	closeToast: function() {
		uni.hideToast();
	},
	storage: {
		set: function(key, value) {
			uni.setStorageSync(key, value);
		},
		get: function(key) {
			const data = uni.getStorageSync(key);
			return data;
		},
		remove: function(key) {
			uni.removeStorageSync(key)
		}
	},
	disposeRes: function(promise, success, fail) {
		try {
			promise.then(res => {
				if (res.code === 200){
					let data = res.data || res;
					 success(data)
				}else {
					toast(res.msg)
					fail(res)
				}
			}).catch(e => {
				console.log(e);
			})
		} catch (e) {

		}
	},
	// 初始化公众号sdk
	initWxSdk(pageUrl, data, jsApiList, sdkObj){
		// 获取sdk信息
		sdkObj.config({
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
			appId: data.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
			timestamp: data.timestamp, // 必填，生成签名的时间戳
			nonceStr: data.nonceStr, // 必填，生成签名的随机串
			signature: data.signature,// 必填，签名，见附录1
			// jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage", "scanQRCode"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
			jsApiList: jsApiList // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
		})
	},
	// px转rpx
	toRpx: function(num, windowWidth){
		return num * 750 / windowWidth
	},
	getUserInfoByMiniProgram: function(loginApi){
		let promise = new Promise((reslove, reject)=>{
			uni.login({
				provider:'weixin',
				success: (res) => {
					let data = {code: res.code}
					uni.getUserInfo({
						success: (res) => {
							// data.iv = res.iv;
							// data.encryptedData = res.encryptedData
							data.sex = res.userInfo.gender
							data.logo = res.userInfo.avatarUrl
							data.uname = res.userInfo.nickName
							//获取信息
							if(!loginApi)return this.toast('登录接口不存在')
							this.disposeRes(loginApi(data), res=>{
								// 传入code在获取手机号时需要用到
								reslove(res)
							})
						},
						fail: (err) => {
							//用户未授权
							this.toast('授权登录失败')
							reject(err)
						}
					})
				},
				fail: (err) => {
					reject(err)
					this.toast('授权登录失败')
				}
			})
		})
		return promise;
	},
	getDateDiff: function(timespan) {
		var dateTime = new Date(timespan * 1000);
		var year = dateTime.getFullYear();
		var month = dateTime.getMonth() + 1;
		var day = dateTime.getDate();
		var hour = dateTime.getHours();
		var minute = dateTime.getMinutes();
		var second = dateTime.getSeconds();
		var now = new Date();
		var now_new = now.getTime(); //typescript转换写法
		var milliseconds = 0;
		var timeSpanStr;
		// 添加0补位
		month = month < 10 ? `0${month}` : month;
		day = day < 10 ? `0${day}` : day;
		hour = hour < 10 ? `0${hour}` : hour;
		minute = minute < 10 ? `0${minute}` : minute;
		second = second < 10 ? `0${second}` : second;
		
		milliseconds = now_new - timespan * 1000;
		if (milliseconds <= 1000 * 60 * 1) {
			timeSpanStr = '刚刚';
		} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
		} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
		} else if (year == now.getFullYear()) {
			timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
		} else {
			timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
		}
		return timeSpanStr;
	}
}
